package models

import (
	"github.com/pkg/errors"
	"math/rand"
	"strconv"
	"time"
)

// Rand 生成订单编号
func Rand() int {
	max := 9999
	min := 1000
	cn := rand.Intn(max-min+1) + min
	return cn + int(time.Now().UnixNano())
}
func CreateOrder(name string, num int) error {
	goods := GoodsJi{
		Name: name,
	}

	err := o.Read(&goods, "name")
	if err != nil {
		return errors.New("技师不存在")
	}
	o.QueryTable("goodsJi").Filter("name", name).One(&goods)
	//计算总金额
	Count := int(goods.Price) * num
	//生成订单编号
	Cn := Rand()
	//生成订单
	order := Order{
		Cn:    strconv.Itoa(Cn),
		Name:  name,
		Price: goods.Price,
		Time:  60 * 60 * 2,
		Num:   num,
		Sum:   Count,
	}
	_, err = o.Insert(&order)
	if err != nil {
		return errors.New("订单生成失败")
	}
	return nil
}
